﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Consultant.Core;

namespace Consultant.Models
{
    public class AdminRepository
    {
        private ProjectDataContext _datacontext;
        public AdminRepository(ProjectDataContext datacontext)
        {
            _datacontext = datacontext;
        }

        public void CreateAdmin(Administrator a)
        {
            _datacontext.Administrators.InsertOnSubmit(a);
            _datacontext.SubmitChanges();
        }
        public bool SeeSite(string site)
        {
            var t = _datacontext.Administrators.SingleOrDefault(e => e.Site == site);
            if (t == null){
                return true;
            }
            else{
                return false;
            }
        }

        public int GetAdminIdBySite(string site)
        {
            return _datacontext.Administrators.SingleOrDefault(e => e.Site == site).id;
        }

        public int GetAdminIdByName(string name)
        {
            return _datacontext.Administrators.SingleOrDefault(e => e.UserName == name).id;
        }

        public IEnumerable<QueryForAdmin> GetUnActivQuery(string adminName)
        {
            return from e in _datacontext.SetOpAtSites
                   from b in _datacontext.Operators
                   where e.AdminId == GetAdminIdByName(adminName)
                   where b.OperatorId == e.OperId
                   where e.Activ == 0 
                   select new QueryForAdmin(e.id, b.FirstName, b.LastName);
        }

        public void AddQuery(int id)
        {
            var q = _datacontext.SetOpAtSites.SingleOrDefault(i => i.id == id);
            q.Activ = 1;
            _datacontext.SubmitChanges();
        }

        public void DeleteQuery(int id)
        {
            var q = _datacontext.SetOpAtSites.SingleOrDefault(i => i.id == id);
            _datacontext.SetOpAtSites.DeleteOnSubmit(q);
            _datacontext.SubmitChanges();
        }
    }
}